package org.example.deal;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.stream.Collectors;

public class Test01 {
    public static void main(String[] args) {
        List<Deal> dealList = new ArrayList<>();
        dealList.add(new Deal(2011, 700));
        dealList.add(new Deal(2011, 800));
        dealList.add(new Deal(2010, 900));
        dealList.add(new Deal(2012, 1000));
        List<Shangren> shangrens = new ArrayList<>();
        shangrens.add(new Shangren("zhangyinlong", "Zheng"));
        shangrens.add(new Shangren("lisi", "SuZhou"));
        shangrens.add(new Shangren("lisi", "ShangHai"));
        shangrens.add(new Shangren("wangwu", "SuZhou"));
        shangrens.add(new Shangren("zhaoliu", "SuZhou"));
        List<Trader> traders = new ArrayList<>();
        traders.add(new Trader("zhangsan", 7677L, "suzhou"));
        traders.add(new Trader("lisi", 7000L, "shanghai"));
        traders.add(new Trader("wangwu", 6000L, "suzhou"));
        traders.add(new Trader("zhaoliu", 8000L, "zhengzhoou"));



/*        List<Deal> a=dealList.stream().filter(deal -> deal.getYear()==2011).sorted(Comparator.comparing(Deal::getDeal)).collect(Collectors.toList());
        System.out.println(a);*/
//        List<String> shangrens1=shangrens.stream()
//                .map(shangren -> shangren.getCity()).distinct()
//                .collect(Collectors.toList());
//        List<String> ab=shangrens.stream().map(shangren -> shangren.getCity()).filter(Shangren::getName).collect(Collectors.toList());
//        System.out.println(ab);
//        11.交易员都在哪些不同的城市工作过?

        // lisi=[SuZhou,ShangHai]
        Map<String, List<String>> personWorkerCityMap = shangrens.stream().collect(
                //groupingBy -> key
                Collectors.groupingBy(Shangren::getName,
                        // value -> List
                        Collectors.mapping(Shangren::getCity, Collectors.toList()))
        );

        System.out.println(personWorkerCityMap);


        //2.java8 之前
        Map<String, List<String>> map2 = new HashMap<>();
        for (Shangren shangren : shangrens) {
            if (map2.containsKey(shangren.getName())) {
                //存放的是城市名称的列表
                List<String> val = map2.get(shangren.getName());
                val.add(shangren.getCity());
                map2.put(shangren.getName(), val);
            } else {
                //存放的是城市名称的列表
                List<String> cities = new ArrayList<>();
                cities.add(shangren.getCity());
                map2.put(shangren.getName(), cities);
            }
        }
        System.out.println("----------");
        System.out.println(map2);


        // ShangHai = [lisi,ZhangSan]

        Map<String, List<String>> cityPersonsMap = shangrens.stream().collect(Collectors.
                groupingBy(Shangren::getCity,
                        Collectors.mapping(Shangren::getName, Collectors.toList())));

        System.out.println("-------");
        System.out.println(cityPersonsMap);

        //12.查找所有来自于剑桥的交易员,并按姓名排序
        shangrens.stream()
                .filter(shangren -> shangren.getCity().equals("SuZhou"))
                .sorted((s1, s2) -> s1.getName().compareTo(s2.getName())).forEach(
                System.out::println
        );

        //13.返回所有交易员的姓名字符串,按字母顺序排序
        shangrens.sort(Comparator.comparing(o -> o.getName()));
        List<String> names = shangrens.stream().map(o -> o.getName()).distinct().collect(Collectors.toList());
        System.out.println(String.join(",", names));
        //14.有没有交易员是在米兰工作的?
        Boolean isWork = shangrens.stream().anyMatch(shangren -> shangren.getCity().equals("SuZhou"));
        System.out.println("是否在苏州工作" + isWork);
        //15.打印生活在剑桥的交易员的所有交易额
        LongSummaryStatistics longSummaryStatistics = traders.stream()
                .filter(t -> "suzhou".equals(t.getCity())).mapToLong(
                        it -> it.getDeal()
                ).summaryStatistics();
        System.out.println("-------------");
        System.out.println(longSummaryStatistics.getSum());


        long tradeDetalTotal = traders.stream().filter(t -> "suzhou".equals(t.getCity())).map(it -> it.getDeal())
                .reduce(Long.valueOf(0), Long::sum);
        System.out.println(tradeDetalTotal);
        //16.所有交易中,最高的交易额是多少?




    }
}
